package com.example.controller;

import com.example.entity.Department;
import com.example.entity.Employee;
import com.example.entity.User;
import com.example.repository.DepartmentRepository;
import com.example.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Collection;
import java.util.List;

@Controller
public class DepartmentController {
    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    DepartmentRepository departmentRepository;

    @RequestMapping("/department")
    public String depList(Model model, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        Collection<Department> departments = departmentRepository.findAllByUserId(loginUser.getId());
        if (departments.size() <= 0){
            session.setAttribute("dep",null);
        }
        model.addAttribute("deps",departments);
        return "dep/dep_list";
    }

    @GetMapping("/dep")
    public String toAddDep(HttpSession session,Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        List<Department> departments = departmentRepository.findAllByUserId(loginUser.getId());
        if (departments.size() <= 0){
            model.addAttribute("dep_id",101);
            return "dep/add";
        }else {
            Department department = departments.get(departments.size() - 1);
            Integer departmentId = department.getId();
            model.addAttribute("dep_id",departmentId+1);
            return "dep/add";
        }

    }

    @PostMapping("/dep")
    public String addDep(Department department, HttpSession session) throws ParseException {
        User loginUser = (User) session.getAttribute("loginUser");
        department.setUserId(loginUser.getId());

        departmentRepository.saveAndFlush(department);
        return "redirect:/department";
    }

    //去部门的修改页面
    @GetMapping("/dep/{id}")
    public String toUpdateDep(HttpSession session,Model model, @PathVariable("id")Integer id){

        User loginUser = (User) session.getAttribute("loginUser");
        //查出原来的数据
        Department department = departmentRepository.findByIdAndUserId(id, loginUser.getId());
        session.setAttribute("dep",department);
        model.addAttribute("dep",department);
        return "dep/update";
    }

    @PostMapping("/dep/{id}")
    public String updateDep(Department department,HttpSession session) throws ParseException {
        User loginUser = (User) session.getAttribute("loginUser");
        Department dep = (Department) session.getAttribute("dep");

        department.setUserId(loginUser.getId());//设置用户id

        department.setIdd(dep.getIdd());
        //调用方法保存员工信息
        departmentRepository.saveAndFlush(department);
        return "redirect:/department";
    }
    //删除部门
    @RequestMapping("/deleteDep/{id}")
    public String deleteDep(HttpSession session,@PathVariable("id")Integer id) throws ParseException {
        //调用业务方法保存员工信息
        User loginUser = (User) session.getAttribute("loginUser");
        departmentRepository.deleteByIdAndUserId(id,loginUser.getId());

        return "redirect:/department";
    }
}
